package io.bdmc.modules.sys.controller;

import io.bdmc.common.utils.Constant;
import io.bdmc.common.utils.R;
import io.bdmc.modules.sys.entity.CascaderData;
import io.bdmc.modules.sys.entity.SysDeptEntity;
import io.bdmc.modules.sys.service.SysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-20 15:23:47
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController {
	@Autowired
	private SysDeptService sysDeptService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:dept:list")
	public List<SysDeptEntity> list() {
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>());

		return deptList;
	}

	@RequestMapping("/list1")
	@RequiresPermissions("sys:dept:list")
	public R list1() {
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>());
		List<SysDeptEntity> firstList = new ArrayList<>();
		for (SysDeptEntity item : deptList) {
			Long c = deptList.stream().filter(s -> s.getDeptId() == item.getParentId()).count();
			if (c == 0) {// 一级菜单
				item.setChildren(deptList.stream().filter(s -> s.getParentId() == item.getDeptId())
						.collect(Collectors.toList()));

				firstList.add(item);
			} else {
				item.setChildren(deptList.stream().filter(s -> s.getParentId() == item.getDeptId())
						.collect(Collectors.toList()));
			}
		}

		return R.ok().put("data", firstList);
	}

	@RequestMapping("/cascader")
	public R cascader() {
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>());
		List<CascaderData> casList = new ArrayList<>();
		List<CascaderData> firstList = new ArrayList<>();
		for (SysDeptEntity item : deptList){
			CascaderData cas = new CascaderData();
			cas.setLabel(item.getName());
			cas.setValue(item.getDeptId());
			cas.setParentId(item.getParentId());
			casList.add(cas);
		}

		for (CascaderData c : casList) {
			Long count = casList.stream().filter(s -> s.getValue() == c.getParentId()).count();
			if (count == 0) {// 一级菜单
				c.setChildren(casList.stream().filter(s -> s.getParentId() == c.getValue())
						.collect(Collectors.toList()));

				firstList.add(c);
			} else {
				c.setChildren(casList.stream().filter(s -> s.getParentId() == c.getValue())
						.collect(Collectors.toList()));
			}
		}

	

		return R.ok().put("data", firstList);
	}

	/**
	 * 选择部门(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:dept:select")
	public R select() {
		List<SysDeptEntity> deptList = sysDeptService.queryList(new HashMap<>());

		// 添加一级部门
		if (getUserId() == Constant.SUPER_ADMIN) {
			SysDeptEntity root = new SysDeptEntity();
			root.setDeptId(0L);
			root.setName("一级部门");
			root.setParentId(-1L);
			root.setOpen(true);
			deptList.add(root);
		}

		return R.ok().put("deptList", deptList);
	}

	/**
	 * 上级部门Id(管理员则为0)
	 */
	@RequestMapping("/info")
	@RequiresPermissions("sys:dept:list")
	public R info() {
		long deptId = 0;
		if (getUserId() != Constant.SUPER_ADMIN) {
			SysDeptEntity dept = sysDeptService.queryObject(getDeptId());
			deptId = dept.getParentId();
		}

		return R.ok().put("deptId", deptId);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{deptId}")
	@RequiresPermissions("sys:dept:info")
	public R info(@PathVariable("deptId") Long deptId) {
		SysDeptEntity dept = sysDeptService.queryObject(deptId);

		return R.ok().put("dept", dept);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:dept:save")
	public R save(@RequestBody SysDeptEntity dept) {
		sysDeptService.save(dept);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:dept:update")
	public R update(@RequestBody SysDeptEntity dept) {
		sysDeptService.update(dept);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:dept:delete")
	public R delete(long deptId) {
		// 判断是否有子部门
		List<Long> deptList = sysDeptService.queryDetpIdList(deptId);
		if (deptList.size() > 0) {
			return R.error("请先删除子部门");
		}

		sysDeptService.delete(deptId);

		return R.ok();
	}

}
